package com.zjitc.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduChapter;

import com.zjitc.serviceedu.entity.EduCourse;
import com.zjitc.serviceedu.entity.EduVideo;
import com.zjitc.serviceedu.entity.chapter.ChapterVo;
import com.zjitc.serviceedu.entity.chapter.VideoVo;
import com.zjitc.serviceedu.mapper.EduChapterMapper;
import com.zjitc.serviceedu.mapper.EduVideoMapper;
import com.zjitc.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjitc.serviceedu.service.EduCourseService;
import com.zjitc.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-09-20
 */
@Service

public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private  EduVideoService videoService;
    @Autowired
    private EduChapterMapper eduChapterMapper;

    @Override
    public List<ChapterVo> getChapterAndChapterTwo(Long courseId) {

        //最终要的到的数据列表
        ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>();

        //获取章节信息
        QueryWrapper<EduChapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id", courseId);
        queryWrapper1.orderByAsc("sort", "id");
        List<EduChapter> chapters = baseMapper.selectList(queryWrapper1);

        //获取课时信息
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        queryWrapper2.orderByAsc("sort", "id");
        List<EduVideo> videos = videoService.list(queryWrapper2);

        //填充章节vo数据
        int count1 = chapters.size();
        for (int i = 0; i < count1; i++) {
            EduChapter eduChapter = chapters.get(i);

            //创建章节vo对象
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            chapterVoArrayList.add(chapterVo);

            //填充课时vo数据
            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();
            int count2 = videos.size();
            for (int j = 0; j < count2; j++) {

                EduVideo eduVideo = videos.get(j);
                if(eduChapter.getId().equals(eduVideo.getChapterId())){

                    //创建课时vo对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoArrayList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoArrayList);
        }

        return chapterVoArrayList;
    }

    @Override
    public boolean deleteChapter(Long chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);//查询有多少条数据
        if (count > 0) {
            throw new AuthException("有数据，不可以删除");
        } else {
            int rows = baseMapper.deleteById(chapterId);
            if (rows > 0) {
                return true;
            }
        }
        return false;
    }




    @Override
    public boolean removeByCourseId(Long id) {
        int rows = eduChapterMapper.deleteByCourseId(id);
        return rows>0;
    }


}
